/*自定义一个string类型，要求有合理的构造函数，
拷贝构造方法、析构函数，输出方法；
class String{
private:
    char *_data;
public:
    String(const char *str  = 0);
    String(const String& rhs);
    ~String();
    const char* data() const;
};
*/
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <cstdlib>
using namespace std;

class myString{
private:
    char *_data;
    int len;
public:
    myString()
    {
        len=0;
        _data = NULL;
    }
    myString(char * rhs)
    {
        len=strlen(rhs);
        _data=new char[len+1];
        memcpy(_data,rhs,len+1);
    }
    ~myString()
    {
        if(len)
        {
            delete []_data;
            _data = NULL;
            len = 0;
        }
    }
    const char* data() const;
   
    void myInput()
    {
        char *p = new char[16];
        char ch='\0';
        int i = 0;
        len = 16;
        while(ch=getchar())
        {
            if(i>=len-1)
            {
                char *q = new char[len*=2];
                memcpy(q,p,i);
                delete []p;
                p = q;
                delete []q;
            }
            p[i]=ch;
            if(ch=='\n')
            {
                break;
            }
            i++;
        }
        len = i;
        _data = p;
    }
    void myOutput()
    {
        if(len==0)
            cout<<"该字符串为空！"<<endl;
        else
        {
            for(int i = 0; i < len; i++)
            {
                cout<<_data[i];
            }
            cout<<endl;
        }
    }
};

int main(int argc,char*argv[])
{
    myString s1;
    cout<<"请输入字符串s1："<<endl;
    s1.myInput();
    cout<<"s1:";
    s1.myOutput();
    cout<<"请输入新的s1:"<<endl;
    s1.myInput();
    cout<<"s1:";
    s1.myOutput();
    return 0;
}